//
//  MTLiveListController.m
//  MeToo
//
//  Created by 俊松何 on 2018/7/9.
//  Copyright © 2018年 tianxie. All rights reserved.
//

#import "MTLiveListController.h"
#import "HBaseLiveController.h"
#import "MTLiveModel.h"
#import "MTLiveListCell.h"
#import "HLiveController.h"
#import "FORScrollViewEmptyAssistant.h"
#import "MTLiveApiManager.h"


@interface MTLiveListController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property(nonatomic,strong) UICollectionView *collectionView;

@property (nonatomic, strong) NSMutableArray *liveArray;

@property (nonatomic, assign) int page;

@end

@implementation MTLiveListController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = nil;
    self.page = 1;
    UIButton *but = [UIButton buttonWithType:UIButtonTypeCustom];
    [but setTitle:@"直播" forState:UIControlStateNormal];
    [but sizeToFit];
    but.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:20];
    [but setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:but];
    self.navigationItem.leftMargin = 15;
    [self.view addSubview:self.collectionView];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.view.mas_left);
        make.right.mas_equalTo(self.view.mas_right);
        make.top.mas_equalTo(self.view.mas_top);
        make.bottom.mas_equalTo(self.view.mas_bottom);
    }];
    
   
    
    
    @weakify(self);
    // 下拉刷刷新
    self.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        @strongify(self);
        self.page = 1;
        [self getDataIsHeaderRefresh:YES];
    }];
    
    // 上拉加载更多
    self.collectionView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        @strongify(self);
        self.page += 1;
        [self getDataIsHeaderRefresh:NO];
    }];
    self.collectionView.mj_footer.automaticallyHidden = YES;
    [self.collectionView emptyViewConfigerBlock:^(FOREmptyAssistantConfiger *configer) {
        configer.emptySubtitle = NSLTLocalizedString(@"还没有人开播，快去直播吧", nil);
        configer.emptySubtitleFont = [UIFont systemFontOfSize:14];
        configer.emptySubtitleColor = UIColorFromRGB(0xcccccc);
        configer.emptyImage = [UIImage imageNamed:@"EmptyBox"];
        configer.shouldDisplay = ^{
            return YES;
        };
    }];
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self getDataIsHeaderRefresh:YES];
}


#pragma mark - 请求所有数据方法
- (void)getDataIsHeaderRefresh:(BOOL)headerRefresh {
    
    [[MTLiveApiManager manager] gettypeLiveListWithpage:self.page typeId:_typeId completion:^(MTLiveTotalModel *livetotalModel, MTError *error) {
        if(error == nil)
        {
            if (headerRefresh) {
                self.liveArray = livetotalModel.list;
                [self.collectionView reloadData];
            } else {
                [self.liveArray addObjectsFromArray:livetotalModel.list];
                [self.collectionView reloadData];
            }
            if (livetotalModel.last) {
                [self.collectionView.mj_footer endRefreshingWithNoMoreData];
            } else {
                [self.collectionView.mj_footer resetNoMoreData];
                [self.collectionView.mj_footer endRefreshing];
            }
            [self.collectionView.mj_header endRefreshing];
        }
        if (headerRefresh) {
            [self.collectionView.mj_header endRefreshing];
        }else
        {
            [self.collectionView.mj_footer endRefreshing];
        }
        
    }];
    
    
}




-(UICollectionView *)collectionView
{
    if(_collectionView == nil)
    {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) collectionViewLayout:layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = [UIColor groupTableViewBackgroundColor];
//        _collectionView.contentInset = UIEdgeInsetsMake(250, 0, 0, 0);
        _collectionView.alwaysBounceVertical = YES;
        [_collectionView registerNib:[UINib nibWithNibName:@"MTLiveListCell" bundle:nil] forCellWithReuseIdentifier:@"MTLiveListCellID"];
    }
    return _collectionView;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.liveArray.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MTLiveListCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MTLiveListCellID" forIndexPath:indexPath];
    cell.model = self.liveArray[indexPath.row];
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    MTLiveModel *topModel = self.liveArray[[self topIndexWithCurrentIndex:indexPath.row]];
    MTLiveModel *bottomModel = self.liveArray[[self bottomIndexWithCurrentIndex:indexPath.row]];
    HLiveController *liveVc = [[HLiveController alloc] initWithType:LiveControllertypeAudience];
    liveVc.livemodel = self.liveArray[indexPath.row];
    liveVc.toplivemodel = topModel;
    liveVc.bottomlivemodel = bottomModel;
    liveVc.livesArray = self.liveArray;
    liveVc.currentIndex = indexPath.row;
    [self.navigationController pushViewController:liveVc animated:YES];
}


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake((WIDTH - 15) / 2, (WIDTH - 15) / 2);
    
    
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(15, 5, 0, 5);
    
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 4;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 1;
}

-(NSInteger)topIndexWithCurrentIndex:(NSInteger)currentIdnex
{
    if(currentIdnex == self.liveArray.count - 1)
    {
        if(self.liveArray.count == 1)
        {
            return currentIdnex;
        }
        return currentIdnex - 1;
    }else if(currentIdnex == 0)
    {
        if(self.liveArray.count == 1)
        {
            return currentIdnex;
        }
        return  self.liveArray.count - 1;
    }else
    {
        return currentIdnex  - 1;
    }
    
}

-(NSInteger)bottomIndexWithCurrentIndex:(NSInteger)currentIdnex
{
    if(currentIdnex == self.liveArray.count - 1)
    {
        return 0;
    }else if(currentIdnex == 0)
    {
        if(self.liveArray.count == 1)
        {
            return currentIdnex;
        }
        return  currentIdnex + 1;
    }else
    {
        return currentIdnex  + 1;
    }
    
}


@end
